<template>
  <transition :name="transitionName">
    <div class="car-item" v-if="selfIndex == currentIndex">
      <slot></slot>
    </div>
  </transition>
</template>

<script setup>
import { getCurrentInstance, ref, watch } from 'vue';

/* 上下文环境 */
const instance = getCurrentInstance();
/* 自己图片的索引值 */
const selfIndex = ref(instance.vnode.key);
/* 显示图片的索引值 */
const currentIndex = ref(instance.parent.setupState.currentIndex);
/* 动画的名称 */
const transitionName = ref('');

/* 监听当前显示图片的索引值 */
watch(() => {
  return instance.parent.setupState.currentIndex
}, (value) => {
  console.log('currentIndex', value);
  currentIndex.value = value;
})
/* 监听点击按钮的方向，来改变切换图片时的动画 */
watch(() => {
  return instance.parent.setupState.transitionName
}, (value) => {
  console.log('transitionName', value);
  transitionName.value = value;
}, { immediate: true })

</script>

<style lang='scss' scoped>
/* Your component styles here */

.car-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}



/* 右划动画 */
.next-enter-active,
.next-leave-active {
  transition: all .3s linear;
}

.next-enter-from {
  transform: translateX(100%);
}

.next-enter-to {
  transform: translateX(0);
}

.next-leave-from {
  transform: translateX(0);
}

.next-leave-to {
  transform: translateX(-100%);
}


/* 左划动画 */
.prev-enter-active,
.prev-leave-active {
  transition: all .3s linear;
}

.prev-enter-from {
  transform: translateX(-100%);
}

.prev-enter-to {
  transform: translateX(0);
}

.prev-leave-from {
  transform: translateX(0);
}

.prev-leave-to {
  transform: translateX(100%);
}
</style>